package com.mc.developmentkit.login;

import android.content.Context;
import android.os.Message;

import com.mc.developmentkit.callback.LoginListener;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by LeBron on 2016/12/19.
 */

public class MCThirdPartyLogin {

    private static final int MSG_AUTH_CANCEL = 1;
    private static final int MSG_AUTH_ERROR = 2;
    private static final int MSG_AUTH_COMPLETE = 3;

    private static MCThirdPartyLogin instance;


    private MCThirdPartyLogin() {
    }

    public static MCThirdPartyLogin getInstance() {
        if (null == instance) {
            instance = new MCThirdPartyLogin();
        }
        return instance;
    }


    public void toLogin(Context context,String loginType,LoginListener loginListener){
        this.loginListener=loginListener;
        ShareSDK.initSDK(context);
        Platform sl = ShareSDK.getPlatform(loginType);
        sl.removeAccount(true);
        ShareSDK.removeCookieOnAuthorize(true);
        sl.setPlatformActionListener(platformActionListener);
        sl.SSOSetting(false);
        sl.showUser(null);
    }

    private LoginListener loginListener;
    private String code;
    private String msg1;
    private String resultStatus;

    PlatformActionListener platformActionListener=new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
            if (action == Platform.ACTION_USER_INFOR) {
                Message msg = new Message();
                msg.what = MSG_AUTH_COMPLETE;
                msg.arg2 = action;
                msg.obj = platform.getDb().exportData();
                sHandler.sendMessage(msg);
            }
        }

        @Override
        public void onError(Platform platform, int action, Throwable throwable) {
            System.out.println("登录失败回调------:"+throwable.toString());
            if (action == Platform.ACTION_USER_INFOR) {
                Message msg = new Message();
                msg.what = MSG_AUTH_ERROR;
                msg.arg2 = action;
                msg.obj = platform.getDb().exportData();
                sHandler.sendMessage(msg);
            }
            throwable.printStackTrace();
        }

        @Override
        public void onCancel(Platform platform, int action) {
            System.out.println("登录取消回调:" + platform.getDb().exportData());
            if (action == Platform.ACTION_USER_INFOR) {
                Message msg = new Message();
                msg.what = MSG_AUTH_CANCEL;
                msg.arg2 = action;
                msg.obj = platform.getDb().exportData();
                sHandler.sendMessage(msg);
            }
        }

        android.os.Handler sHandler = new android.os.Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_AUTH_COMPLETE:
                        code = "1";
                        msg1="登录成功";
                        resultStatus = msg.obj.toString();
                        break;
                    case MSG_AUTH_ERROR:
                        code = "2";
                        msg1="登录失败";
                        resultStatus = msg.obj.toString();
                        break;
                    case MSG_AUTH_CANCEL:
                        code = "3";
                        msg1="登录取消";
                        resultStatus = msg.obj.toString();
                        break;
                }
                loginListener.loginResult("{\"code\":\"" + code + "\",\"msg\":\""  + msg1 + "\",\"info\":" + resultStatus+ "}");
            }
        };
    };

}
